package com.sd.util;

import org.apache.http.util.EncodingUtils;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.view.ViewGroup.LayoutParams;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.alex.log.ALog;
import com.sd.activity.BookStoreActivity;
import com.sd.app.AppData;
import com.sd.database.ScanItem;

public class WebUtil {

	private static WebView mWebView;
	private static Context mContext;
	
	public static String getSid(String url) {
		StringBuffer sbuf = new StringBuffer();

		String[] sp = url.split("&sid=");
		sbuf.append(sp[0]);

		if (sp.length > 1) {
			int start0 = sp[1].indexOf("&");
			if (start0 >= 0) {
				sbuf.append(sp[1].substring(start0));
			}
		}
		sbuf.append("&from=");
		sbuf.append(AppData.getInstance().mCNChannel);
		sbuf.append("&sid=");
		String text = null;
		if (BookStoreActivity.user != null) {
			sbuf.append(BookStoreActivity.user);
			text = sbuf.toString();
			//text = String.format(sbuf.toString(), user);
		}
		ALog.e("url:" + text);
		return text;
	}
	
	private static HtmlFinish mLisenter;
	private static WebViewClientDemo mClientDeom;
	private static InJavaScriptLocalObj mJavaScript;
	static{
		mClientDeom = new WebViewClientDemo();
		mJavaScript =  new InJavaScriptLocalObj();
	}
	
	public static void getHtml(Context context,HtmlFinish listener,String postUrl,String data){
		mContext = context;
		mWebView = new WebView(context);
		// CookieManager.getInstance().removeExpiredCookie();
		// 设置支持JavaScript等
		WebSettings mWebSettings = mWebView.getSettings();
		mWebSettings.setJavaScriptEnabled(true);
		mWebSettings.setLightTouchEnabled(true);
		mWebSettings.setSupportZoom(false);
		mWebView.setHapticFeedbackEnabled(false);
		mWebView.addJavascriptInterface(mJavaScript, "local_obj");
		mWebView.setWebViewClient(mClientDeom);
		mLisenter = listener;
		mWebView.postUrl(postUrl, EncodingUtils.getBytes(data,"BASE64"));
		
	}
	
	public static WebView getWevView(Context context,String url){
		mContext = context;
		mWebView = new WebView(context);
		mWebView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		// CookieManager.getInstance().removeExpiredCookie();
		// 设置支持JavaScript等
		WebSettings mWebSettings = mWebView.getSettings();
		mWebSettings.setJavaScriptEnabled(true);
		mWebSettings.setLightTouchEnabled(true);
		mWebSettings.setSupportZoom(false);
		mWebView.setHapticFeedbackEnabled(false);
		mWebView.setWebViewClient(mClientDeom);
		mWebView.loadUrl(url);
		return mWebView;
	}
	
	final static class WebViewClientDemo extends WebViewClient {

		
		// 在WebView中而不是默认浏览器中显示页面
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			String tempUrl = url;
			ALog.e(url);
			if(url.contains("sms:")){
				Uri uri = Uri.parse(url);
				Intent it = new Intent(Intent.ACTION_SENDTO, uri);
				int start = url.indexOf("body=") + 5;
				it.putExtra("sms_body", url.substring(start));
				mContext.startActivity(it);
			} else if(url.contains("wtai:")){
				
				int start = url.indexOf("wtai://wp/mc;") + 13;
				Uri uri = Uri.parse("tel:"+url.substring(start));
				Intent it = new Intent(Intent.ACTION_DIAL, uri);
				mContext.startActivity(it);
			} else{
				
				tempUrl = WebUtil.getSid(url);
				view.loadUrl(tempUrl);
			}
			return true;
		}

		@Override
		public void onPageStarted(WebView view, String url, Bitmap favicon) {
			// TODO Auto-generated method stub
			super.onPageStarted(view, url, favicon);
		}

		@Override
		public void onPageFinished(WebView view, String url) {
			// TODO Auto-generated method stub
			ScanItem item = new ScanItem();
			item.name = view.getTitle();
			item.url = view.getUrl();
			view.loadUrl("javascript:window.local_obj.showSource('<head>'+" +
            "document.getElementsByTagName('html')[0].innerHTML+'</head>');");
			super.onPageFinished(view, url);
		}

		@Override
		public void onLoadResource(WebView view, String url) {
			// TODO Auto-generated method stub
			super.onLoadResource(view, url);
		}

		@Override
		public void onReceivedError(WebView view, int errorCode,
				String description, String failingUrl) {
			// TODO Auto-generated method stub
			LoadingProgressDialog.dismiss();
			super.onReceivedError(view, errorCode, description, failingUrl);
		}
	}
	
	final static class InJavaScriptLocalObj {
        public void showSource(String html) {
            ALog.e("HTML"+html);
            mLisenter.onFinish(html);
        }
    }

	
}
